$.ready(function () {
    $(".button").bind('click',function(){
       $("input[name='submit']").val($(this).attr("v"));
    });
    $(".datepicker").datepicker({ dateFormat: "yy-mm-dd" });



    $("#btnSubmit").bind('click',function(){
        var arrObj = returnArrayObject();
        var result = validateEmpty(arrObj);
        return result;
    });
    function returnArrayObject(){//组合验证数组
        var arrComputer = $("select[name='computer[]']");
        var arrWorkSpace = $("select[name='work_space[]']");
        var arrSeat = $("input[name='seat[]']");
        var arrComputerCode = $("input[name='computer_code[]']");
        var len = arrComputer.length;
        var arrObj = new Array();
        for(var i=0;i<len;i++){
            var obj = {};
            obj.computer = arrComputer[i];
            obj.workSpace = arrWorkSpace[i];
            obj.seat = arrSeat[i];
            obj.computerCode = arrComputerCode[i];
            obj.state = false;
            arrObj.push(obj);
        }
        return arrObj;
    }
    /**
     *
     * @param arr
     */
    function validateEmpty(arr){
        var firstObj = null;
        var result = true;
        for(var i= 0,len = arr.length;i<len;i++){
            var obj = arr[i];
            if(!$(obj.computer).val()){
                $(obj.computer).css("borderColor","red");
                firstObj = firstObj ? firstObj: $(obj.computer);
                result = false;
            }else{
                $(obj.computer).css("borderColor","#d3d3d4");
            }
            if(!$(obj.workSpace).val()){
                $(obj.workSpace).css("borderColor","red");
                firstObj = firstObj ? firstObj: $(obj.workSpace);
                result = false;
            }else{
                $(obj.workSpace).css("borderColor","#d3d3d4");
            }
            if(!$(obj.seat).val()){
                $(obj.seat).css("borderColor","red");
                firstObj = firstObj ? firstObj: $(obj.seat);
                result = false;
            }else{
                $(obj.seat).css("borderColor","#d3d3d4");
            }
            if(!$(obj.computerCode).val()){
                $(obj.computerCode).css("borderColor","red");
                firstObj = firstObj ? firstObj: $(obj.computerCode);
                result = false;
            }else{
                $(obj.computerCode).css("borderColor","#d3d3d4");
            }

        }
        if(firstObj){
            firstObj.focus();
        }
        return result;
    }
    $("select[name='computer[]']").bind('change',function(){
       var obj =$(this);
        if(!obj.val()){
            obj.css("borderColor","red");
            return;
        }
        obj.css("borderColor","#d3d3d4");
    });
    $("select[name='computer[]']").bind('blur',function(){
        var obj =$(this);
        if(!obj.val()){
            obj.css("borderColor","red");
        }
    });
    $("select[name='work_space[]']").bind('change',function(){
        var obj =$(this);
        if(!obj.val()){
            obj.css("borderColor","red");
            return;
        }
        obj.css("borderColor","#d3d3d4");
    });
    $("select[name='work_space[]']").bind('blur',function(){
        var obj =$(this);
        if(!obj.val()){
            obj.css("borderColor","red");
        }
    });
    $("input[name='seat[]']").bind('keyup',function(){
       var obj = $(this);
        if(!obj.val()){
            obj.css("borderColor","red");
            return;
        }
        obj.css("borderColor","#d3d3d4");
    });
    $("input[name='seat[]']").bind('blur',function(){
        var obj = $(this);
        if(!obj.val()){
            obj.css("borderColor","red");
            return;
        }
        obj.css("borderColor","#d3d3d4");
    });
    $("input[name='computer_code[]']").bind('keyup',function(){
        var obj = $(this);
        if(!obj.val()){
            obj.css("borderColor","red");
            return;
        }
        obj.css("borderColor","#d3d3d4");
    });
    $("input[name='computer_code[]']").bind('blur',function(){
        var obj = $(this);
        if(!obj.val()){
            obj.css("borderColor","red");
            return;
        }
        obj.css("borderColor","#d3d3d4");
    });




}());
